/*
 *The code is written by NaLi, All rights reserved.
 */
package com.nali.dal.statement.selectupsert;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.InitializingBean;

import com.nali.dal.statement.function.Function;
import com.nali.dal.statement.function.FunctionArgument;
import com.nali.dal.statement.function.FunctionBuilder;
import com.nali.dal.statement.query.SelectStatement;
import com.nali.dal.statement.util.DalCollectionUtils;

/**
 * @author gavin Created on 2011-4-11
 */
public class SelectIntoStatement extends SelectStatement implements InitializingBean {//TODO 改名
	
	private String into;
	private List<String> values = Collections.emptyList();
	private List<String> identitfiedBy = Collections.emptyList();
	private List<String> count = Collections.emptyList();
	private List<String> sum = Collections.emptyList();
	private List<String> set = Collections.emptyList();
	private boolean needIdentified;
	
	public List<String> getCount() {
		return count;
	}

	public void setCount(List<String> count) {
		this.count = count;
	}

	public List<String> getSum() {
		return sum;
	}

	public void setSum(List<String> sum) {
		this.sum = sum;
	}

	public List<String> getSet() {
		return set;
	}

	public void setSet(List<String> set) {
		this.set = set;
	}

	public String getInto() {
		return into;
	}

	public void setInto(String into) {
		this.into = into;
	}

	public List<String> getValues() {
		return values;
	}

	public void setValues(List<String> values) {
		this.values = values;
	}

	public List<String> getIdentitfiedBy() {
		return identitfiedBy;
	}

	public void setIdentitfiedBy(List<String> identitfiedBy) {
		this.identitfiedBy = identitfiedBy;
	}

	@Override
	public void afterPropertiesSet() throws Exception {
		if(StringUtils.isEmpty(into)) {
			throw new IllegalArgumentException("Into table must not be empty");
		}
		
		
		if (!DalCollectionUtils.isEmpty(values)) {
			this.set = new ArrayList<String>();
			this.count = new ArrayList<String>();
			this.sum = new ArrayList<String>();
			for (String value : values) {
				Function function = FunctionBuilder.complie(value);
				String name = function.getName();
				List<FunctionArgument> arguments = function.getArgConfigs();
				if (!DalCollectionUtils.isEmpty(arguments)) {
					if (arguments.size() == 1) {
						FunctionArgument functionArgument = arguments.get(0);
						String column = functionArgument.getColumn();
						if (StringUtils.isEmpty(name)) {
							this.set.add(column);
						} else if ("count".equalsIgnoreCase(name)) {
							this.count.add(column);
							needIdentified = true;
						} else if ("sum".equalsIgnoreCase(name)) {
							this.sum.add(column);
							needIdentified = true;
						} else {
							throw new IllegalArgumentException("Illegal value: " + column + ", not support by system");
						}
					} else {
						throw new IllegalArgumentException("Not support mutiple arguments in recent value statment.");
					}
				}
			}
		} else {
			this.set = select;
		}
		
		if(needIdentified) {
			if(DalCollectionUtils.isEmpty(identitfiedBy)) {
				throw new IllegalArgumentException("IdentitifiedBy must not be empty in your usecase");
			}
		}

	}
}
